package com.jy.datapipeline.importserver.controller;

import com.jy.datapipeline.common.vo.ReturnVo;
import com.jy.datapipeline.importserver.common.dto.ImportDataSourceNamesDto;
import com.jy.datapipeline.importserver.service.ImportDataSourceService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名 ImportDataSourceController
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/4/17 13:12
 * @版本 1.0
 */
@RestController
@RequestMapping("/target-data-source")
public class ImportDataSourceController {

    private final ImportDataSourceService importDataSourceService;

    public ImportDataSourceController(ImportDataSourceService importDataSourceService) {
        this.importDataSourceService = importDataSourceService;
    }

    @GetMapping("/query-all-names")
    public ReturnVo queryDataSourceNames() {
        List<ImportDataSourceNamesDto> namesDtos = importDataSourceService.queryDataSourceNames();
        return ReturnVo.ok().put(namesDtos);
    }

    @GetMapping("/query-schemas")
    public ReturnVo queryDataSourceSchemas(@RequestParam String dataSourceId) throws SQLException {
        return ReturnVo.ok().put(importDataSourceService.queryDataSourceSchemas(dataSourceId));
    }

    @GetMapping("/query-tables")
    public ReturnVo queryDataSourceTables(@RequestParam String dataSourceId,
                                          @RequestParam String schema,
                                          @RequestParam String searchType,
                                          String tblName,
                                          Integer endIndex) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        map = importDataSourceService.queryTables(dataSourceId, schema, searchType, tblName, endIndex, map);
        return ReturnVo.ok().put(map.get("ruleTblList")).put("count", map.get("ruleTblCount"));
    }

    @GetMapping("/query-columns")
    public ReturnVo queryDataSourceColumns(@RequestParam String dataSourceId, @RequestParam String schema, @RequestParam String tableName) throws SQLException {
        List<Map<String,Object>> columns = importDataSourceService.queryDataSourceColumns(dataSourceId, schema, tableName);
        return ReturnVo.ok().put(columns);
    }
}